﻿using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NHibernateExample.Models
{
    /// <summary>
    /// The Model class of links
    /// Basic CRUD support is implemented
    /// </summary>
    public class Links
    {
        /// <summary>
        /// Get all data from the links list
        /// </summary>
        /// <param name="session"></param>
        public static void Read(ISession session)
        {
            IQuery query = session.CreateQuery("FROM Links");
            IList<Content.Links> results = query.List<Content.Links>();

            if (results.Any())
            {
                Console.WindowWidth = 90;
                const string header = "{0, -9} {1}";
                foreach (Content.Links item in results)
                {
                    Console.WriteLine(header, "ID", item.ID);
                    Console.WriteLine(header, "URL", item.URL);
                    Console.WriteLine(header, "Created", item.Created.ToShortDateString());
                    Console.WriteLine(header, "Modified", item.Modified.ToShortDateString());
                    Console.WriteLine(header, "Comments", item.Comments);
                    Console.WriteLine(new string('-', 20));
                }
            }
            else
            {
                Console.WriteLine("Could not fetch any lists");
            }
        }

        /// <summary>
        /// Get a single entry from the links list
        /// </summary>
        /// <param name="session"></param>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public static Content.Links Read(ISession session, int itemId)
        {
            IQuery query = session.CreateQuery("FROM Links WHERE ID = :ID").SetParameter("ID", itemId);
            return query.UniqueResult<Content.Links>();
        }

        /// <summary>
        /// Insert new post in the links list
        /// </summary>
        /// <param name="session"></param>
        /// <param name="item"></param>
        /// <returns>The created object</returns>
        public static Content.Links Create(ISession session, Content.Links item)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                var ret = session.Save(item);
                transaction.Commit();
            }
            Console.WriteLine("Saved link to SharePoint with ID {0}", item.ID);
            Console.WriteLine(new string('-', 20));

            return item;
        }

        /// <summary>
        /// Update a post in the links list
        /// </summary>
        /// <param name="session"></param>
        /// <param name="item"></param>
        /// <returns>The updated object</returns>
        public static Content.Links Update(ISession session, Content.Links item)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(item);
                transaction.Commit();
            }
            Console.WriteLine("Updated link with ID {0}", item.ID);
            Console.WriteLine(new string('-', 20));

            return item;
        }

        public static void Delete(ISession session, Content.Links item)
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(item);
                transaction.Commit();
            }
            Console.WriteLine("Deleted link with ID {0}", item.ID);
            Console.WriteLine(new string('-', 20));
        }
    }
}
